import { TOKEN } from '@/constants'
import router from '@/router'
import { getItem, removeAllItem, setItem } from '@/utils/storage'
import { loadUserInfo } from '@/api/sys'

export default {
  namespaced: true,
  state: () => ({
    token: getItem(TOKEN),
    userInfo: {}
  }),
  mutations: {
    setToken(state, token) {
      state.token = token
      setItem(TOKEN, token)
    },
    setUserInfo(state, userInfo) {
      state.userInfo = userInfo
    }
  },
  actions: {
    logout() {
      this.commit('user/setToken', '')
      this.commit('user/setUserInfo', {})
      removeAllItem()
      router.replace('/login')
    },
    async getUserInfo() {
      const res = await loadUserInfo()
      // console.log(res)
      this.commit('user/setUserInfo', res)
    }
  }
}
